package io.gitee.hechenl.fastiotreport.service.impl;

import io.gitee.hechenl.fastiotreport.domain.model.ConfigMetric;
import io.gitee.hechenl.fastiotreport.mapper.ConfigMetricMapper;
import io.gitee.hechenl.fastiotreport.service.IEnergyHalfService;
import io.gitee.hechenl.fastiotreport.service.IMetricService;
import io.gitee.hechenl.fastiotreport.service.IProductService;
import io.gitee.hechenl.fastiotreport.util.ExprCalculator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MetricServiceImpl implements IMetricService {

    @Autowired
    private ConfigMetricMapper configMetricMapper;
    @Autowired
    private IEnergyHalfService energyService;
    @Autowired
    private IProductService productService;



    @Override
    public Map<ConfigMetric, BigDecimal> values(List<Long> metricIds, LocalDateTime start, LocalDateTime end) {
        ExprCalculator.EnergySupplier energySupplier = pointIds -> energyService.sumValue(pointIds, start, end);
        ExprCalculator.ProductSupplier productSupplier = unit -> productService.getProduct(unit, start, end);

        Map<ConfigMetric, BigDecimal> map = new HashMap<>();
        List<ConfigMetric> configMetrics = configMetricMapper.selectBatchIds(metricIds);
        for (ConfigMetric config : configMetrics) {
            map.put(config, ExprCalculator.calculateMetric(config, energySupplier, productSupplier));
        }
        return map;
    }

}
